//增加快递单
var app = getApp()
Page({
  data: {
    shipperArray: [ {
        id: 'EMS',
        name: 'EMS'
      },
      {
        id: 'SAL',
        name: 'SAL'
      },
      {
        id: 'AIR',
        name: '航空便'
      },
      {
        id: 'SHIP',
        name: '船便'
      }],
    weightArray: [2, 5, 15,16,17,18],
    form: {
      postid: '',
      shipper: 'EMS',
      weight: '',
      remark: '',
    },
    shipperIndex: 0,
    weightIndex: 0,
  },
  onLoad: function () {
      console.log(app.WxService);
  		this.WxValidate = app.WxValidate({
  			postid: {
  				required: true,
  				minlength: 8,
  				maxlength: 16,
  			},
  			remark: {
        maxlength: 30,
  			},
  		}, {
  				postid: {
  					required: '请输入快递单号',
  				},
  			})

  		this.order = app.HttpResource('/order/:id', { id: '@id' })
  },
  bindShipperChange: function(e) {
    this.setData({
      shipperIndex: e.detail.value,
    })
  },
  bindWeightChange: function(e) {
    this.setData({
      weightIndex: e.detail.value,
    })
  },
	submitForm(e) {
    console.log(e);
		const params = e.detail.value

		if (!this.WxValidate.checkForm(e)) {
			const error = this.WxValidate.errorList[0]
			app.WxService.showModal({
				title: '友情提示',
				content: `${error.msg}`,
				showCancel: !1,
			})
			return false
		}

		this.order.saveAsync(params).then(data => {
		  this.showToast('添加成功',data)
		})
	},
	showToast(message,data) {
		app.WxService.showToast({
			title: message,
			icon: 'success',
			duration: 1500,
		}).then(() => {
      app.WxService.navigateTo('/pages/query/address/index', {
        id: data.id
      })
    })
	},
  scanCode() {
    wx.scanCode({
      success: (res) => {
        this.setData({
          'form.postid': res.result,
        });
      }
    })
  }
})
